package com.xuhao.didi.oksocket;

import com.xuhao.didi.oksocket.data.LogBean;
import com.xuhao.didi.socket.client.sdk.OkSocket;
import com.xuhao.didi.socket.client.sdk.client.connection.IConnectionManager;
import com.xuhao.didi.socket.common.interfaces.common_interfacies.server.IServerManager;
import com.xuhao.didi.socket.common.interfaces.utils.TextUtils;
import com.xuhao.didi.socket.server.action.ServerActionAdapter;
import org.junit.Test;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;

import static org.junit.Assert.assertEquals;

public class ExampleOhosTest {

    @Test
    public void isConnect() {
        IConnectionManager open = OkSocket.open("10.0.5.2", 8080);
        open.connect();
        boolean connect = open.isConnect();
        assertEquals(connect, false);
    }
    @Test
    public void logbean(){
        LogBean logBean = new LogBean(System.currentTimeMillis(),"test");
        assertEquals(logBean.mLog,"test");
    }
    @Test
    public void IsTest(){
        assertEquals(TextUtils.isEmpty("123"),false);
    }
    @Test
    public void getHostAddresss(){
        ArrayList<InetAddress> foundAddresses = new ArrayList<>();

        Enumeration<NetworkInterface> ifaces;
        try {
            ifaces = NetworkInterface.getNetworkInterfaces();
            while (ifaces.hasMoreElements()) {
                NetworkInterface iface = ifaces.nextElement();
                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                        foundAddresses.add(addr);
                    }
                }
            }
            InetAddress inetAddress = foundAddresses.get(0);
            assertEquals(inetAddress.getHostAddress(),"10.0.5.2");
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void ServiceManage(){
        IServerManager manager = OkSocket.server(8080).registerReceiver(new ServerActionAdapter() {
            @Override
            public void onServerListening(int serverPort) {
                super.onServerListening(serverPort);
                assertEquals(serverPort,8080);
            }
        });
    }
}